package com.hertz.iris.view.alert;

import java.util.List;

import javax.inject.Inject;

import android.os.Bundle;
import android.os.PowerManager.WakeLock;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.WindowManager;
import android.widget.Button;
import butterknife.InjectView;
import butterknife.OnClick;

import com.hertz.iris.Injector;
import com.hertz.iris.R;
import com.hertz.iris.core.MatchAlarmService;
import com.hertz.iris.pojo.MatchAlarm;
import com.hertz.iris.pojo.ResponseIsAlert;
import com.hertz.iris.ui.IrisFragmentActivity;
import com.hertz.iris.util.BundleKeys;
import com.hertz.iris.util.MediaPlayerHelper;

public class MatchAlarmAlertActivity extends IrisFragmentActivity {

	@InjectView(R.id.stop_btn)
	Button stopBtn;
	@Inject
	MatchAlarmService matchAlarmService;
	private MatchAlarm ma;
	WakeLock wakeLock;
	
	private MediaPlayerHelper mediaPlayerHelper;
	
	@Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        Injector.inject(this);
        
        AlarmAlertWakeLock.acquire(this);
        setContentView(R.layout.match_alert); 
        addMatchFragment();
        // TODO clearFlags
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
                | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON 
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        Bundle bundle = getIntent().getExtras();
        List<ResponseIsAlert> r = (List<ResponseIsAlert>)bundle.getSerializable(BundleKeys.MatchAlarmBundleKeys.MATCH_ALARM_ALERT_KEY);
        ma = r.get(0).getIrisNbaAlarm();
        mediaPlayerHelper = new MediaPlayerHelper(this);
        mediaPlayerHelper.playRingtone(ma.getRingUri(), (float)ma.getVolume() / 100,ma.getWave());
        
    }
	
	@OnClick(R.id.stop_btn)
	void stopBtnOnClick(){
		mediaPlayerHelper.stopRingtone();
		AlarmAlertWakeLock.release();
		finish();
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	private void addMatchFragment() {
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.match_nba_alarm_alert,new MatchAlarmAlertFragment(), MatchAlarmAlertFragment.class.getSimpleName());
        ft.commit();
	}
	
}
